#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "fw/comm/inc/serial.h"
#include "cfg.h"
#include "lens.h"
#include "mpp.h"
#include "spi_ms41929.h"

// for CPU that not implement lensxxx.c
#if defined(GSF_CPU_3516d) && !defined(GSF_CPU_3559)
// #warning "lens not implement"
static int __lens_init(gsf_lens_ini_t *ini) { 
  zoom_lens_init();
 return -1;
}
static int __lens_start(int ch, char *ttyAMA) { return -1; }
static int __lens_ircut(int ch, int dayNight) { return -1; }
static int __lens_zoom(int ch, int dir, int speed)
{
    printf("zoom,ch:%d,dir:%d,speed:%d\n", ch, dir, speed);
    if (dir == 0)
    {
       spi_ms41929_write(0x29, 0x040a); 
       spi_ms41929_write(0x24, 0x0502); 
    }
    else
    {      
        spi_ms41929_write(0x29, 0x050a);
        spi_ms41929_write(0x24, 0x0402);
    }
    spi_ms41929_write(0x2a, 0x3e8);//1000:3e8 ,2000:7d0,4000:fa0, 200:c8,the less,the faster,1000 *111ns=111us=0.1ms
    spi_ms41929_write(0x25, 0x7d0);//the less,the faster
    pwm0_enable();
    return 0;
}; // 
static int __lens_focus(int ch, int dir, int speed) 
{
  printf("focus,ch:%d,dir:%d,speed:%d\n", ch, dir, speed);
   //about 9us during sending a data through spi
  
  //  zoom_lens_init();
    if (dir == 0)
    {
         spi_ms41929_write(0x24, 0x0502); 
    }
    else
    {
       spi_ms41929_write(0x24, 0x0402);
    }
    spi_ms41929_write(0x25, 0x7d0);//1000:3e8 ,200:7d0, 200:c8,the less,the faster
/*
在初始化时，
spi_ms41929_write(0x21, 0x0085);//PLS1 ，监控输出脉冲A
此时示波器显示为10个高低电平都为0.444ms的脉冲，256细分时，每4步改变一次，所以实际应为0.111ms一个周期，与INTCTAB设置相符。
如果spi_ms41929_write(0x21, 0x0087);//PLS1 ，A通道完整的脉冲输出
此时示波器显示为周期为10ms，这是pwm0设置的，高点平为8.8ms，这是上面10步需要用的时间，所以这个测试很重要，既需要让高电平尽量占用10ms周期，又要留一点DT1和DT2的时间
*/

    pwm0_enable();
    return 0;
}; // 
static int __lens_stop(int ch)            
{
    pwm0_disable();
     spi_ms41929_write(0x24, 0x0);
     spi_ms41929_write(0x29, 0x0);
    return 0;
}; // 
 static int __lens_cal(int ch) { return -1; }
 static int __uart_open(char *ttyAMA, int baudrate) { return -1; }
 static int __uart_write(unsigned char *buf, int size) { return -1; }

int (*gsf_lens_start)(int ch, char *ttyAMA) = __lens_start;
int (*gsf_lens_ircut)(int ch, int dayNight) = __lens_ircut;
int (*gsf_lens_zoom)(int ch, int dir, int speed) = __lens_zoom;
int (*gsf_lens_focus)(int ch, int dir, int speed)= __lens_focus;
int (*gsf_lens_stop)(int ch)= __lens_stop;
int (*gsf_lens_cal)(int ch) = __lens_cal;
int (*gsf_uart_open)(char *ttyAMA, int baudrate) = __uart_open;
int (*gsf_uart_write)(unsigned char *buf, int size) = __uart_write;
int (*gsf_lens_init)(gsf_lens_ini_t *ini) = __lens_init;

#endif

// int (*gsf_lens_zoom)(int ch, int dir, int speed)=
// {
//     printf("zoom,ch:%d,dir:%d,speed:%d\n",ch,dir,speed);
//     if (dir == 0)
//     {
//         spi_ms41929_write(0x24, 0x045f);
//     }
//     else
//     {
//         spi_ms41929_write(0x24, 0x055f);
//     }
//     spi_ms41929_write(0x25, 0x05ff * speed);

//     pwm0_enable();

//     return 1;
// }

// int *gsf_lens_focus(int ch, int dir, int speed)
// {
//      printf("focus,ch:%d,dir:%d,speed:%d\n",ch,dir,speed);
//     if (dir == 0)
//     {
//         spi_ms41929_write(0x29, 0x045f);
//     }
//     else
//     {
//         spi_ms41929_write(0x29, 0x055f);
//     }
//     spi_ms41929_write(0x2a, 0x05ff * speed);

//     pwm0_enable();

//     return 1;
// }

// int *gsf_lens_stop(int ch)
// {
//     pwm0_disable();
// };